package com.android.canvasproject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.android.canvasproject.adapter.DetailPersonDrawerAdapter;
import com.android.canvasproject.model.entity.Person;
import com.android.canvasproject.view.fragments.AddCanvasDialog;
import com.android.canvasproject.view.fragments.HomeFragment;

public class MainActivity extends Activity {

	private DrawerLayout drawerLayout;
	private ListView drawerList;
	private ActionBarDrawerToggle drawerToggle;

	@SuppressWarnings("unused")
	private CharSequence drawerTitle;
	private CharSequence title;

	private List<Person> personDrawers;
	private DetailPersonDrawerAdapter detailAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		title = drawerTitle = getTitle();
		drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		drawerList = (ListView) findViewById(R.id.list_slidermenu);

		//TODO OBJETO ESTATICO
		Person p = new Person();
		p.setName("DIOGO MEDEIROS");
		p.setAcademicTraining("Análise e Desenvolvimento de Sistemas");
		p.setCity("João Pessoa");
		p.setDescriptionLastActivity("Desenvolvimento do sistema de gestão financeira do município de João Pessoa – PB; \r\nDesenvolvimento de testes de aceitação com a ferramenta Selenium Web-Drive em Java; \r\nCriação de scripts de teste com uso do Easy Accept para técnica do TDD; \r\nUso de tecnologias para o desenvolvimento como: JavaEE, Jsf, Ejb, Hibernate, PostgreSQL, Eclipse, Jboss, MVC Pattern e SVN;");
		p.setEndingLastJob(new Date());
		p.setStartingLastJob(new Date());
		p.setLocationLastJob("João Pessoa, Paraíba, Brasil");
		p.setEnterprise("Prefeitura Municipal de João Pessoa");
		p.setOccupation("Analista Desenvolvedor de Sistemas");
		p.setState("Paraíba");
		p.setTeachingInstitution("Faculdade Estácio IDez");

		personDrawers = new ArrayList<Person>();
		personDrawers.add(p);

		detailAdapter = new DetailPersonDrawerAdapter(getApplicationContext(), personDrawers);
		drawerList.setAdapter(detailAdapter);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, 
        		R.string.app_name, R.string.app_name){

        	@Override
        	public void onDrawerClosed(View drawerView) {
        		getActionBar().setTitle(title);
        		invalidateOptionsMenu();
        	}

        	@Override
        	public void onDrawerOpened(View drawerView) {
        		getActionBar().setTitle(title);
        		invalidateOptionsMenu();
        	}
        };

        drawerLayout.setDrawerListener(drawerToggle);
        if(savedInstanceState == null)
        	fillWithFragment();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_activity_actions, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(drawerToggle.onOptionsItemSelected(item))
			return true;

		switch (item.getItemId()) {
		case R.id.action_search:
			Log.i("[ACTION SEARCH]", " ... ");
			return true;
			
		case R.id.action_new:
			Log.i("[ACTION NEW CANVAS]", " ... ");
			DialogFragment dialog = new AddCanvasDialog();
			dialog.show(getFragmentManager(), "addCanvasDialog");
			return true;
			
		case R.id.action_refresh:
			Log.i("[ACTION REFRESH]", " ... ");
			return true;

		case R.id.action_settings:
			Log.i("[ACTION SETTINGS]", " ... ");
			return true;

		case R.id.action_send_comment:
			Log.i("[ACTION FEEDBACK]", " ... ");
			return true;

		case R.id.action_help:
			Log.i("[ACTION HELP]", " ... ");
			return true;

		default:
			return false;
		}
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		for(int i = 0; i < menu.size(); i++)
			menu.getItem(i).setVisible(!drawerLayout.isDrawerOpen(drawerList));

//		menu.findItem(R.id.action_refresh).setVisible(false);

		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public void setTitle(CharSequence titleParam) {
		title = titleParam; 
		getActionBar().setTitle(title);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		drawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		drawerToggle.onConfigurationChanged(newConfig);
	}
	
	public void fillWithFragment(){
		Fragment fragment = new HomeFragment();
		if(fragment != null)
			getFragmentManager().beginTransaction().replace(R.id.frame_container, fragment).commit();
	}
}